package com.thinkgem.jeesite.cn.yy.action;

import com.thinkgem.jeesite.cn.yy.entity.ProInfo;
import com.thinkgem.jeesite.cn.yy.service.ProInfoService;
import com.thinkgem.jeesite.cn.yy.utils.Paging;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 项目信息Action
 * Created by Administrator on 2017/11/17 0017.
 * yzw
 */
@Controller
@RequestMapping("Projects")
public class ProjectQueryAction{

    @Autowired
    private ProInfoService proInfoService;


    /**
     * 获取所有项目的信息
     * 并分页显示数据
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("getProjects")
    public void getProjects(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = proInfoService.getProInfosPage();
        if(count==0){
            response.getWriter().println("{\"message\":\"没有项目信息(也许没有权限)\"}");
            return;
        }
        int first=Integer.parseInt(request.getSession().getServletContext().getAttribute("pageSize").toString());
        Paging paging = new Paging(0,first,count);
        List<ProInfo> proInfoList = proInfoService.getProInfos(paging);
        proInfoService.saveGetProjectAction();
        if(proInfoList != null){
            response.getWriter().println(JSONArray.fromObject(proInfoList).toString());
            response.getWriter().println(JSONArray.fromObject(paging).toString());
        }else{
            response.getWriter().println("{\"message\":\"没有项目信息(也许没有权限)\"}");
        }

    }

    /**
     * 通过上一下下一页
     * 点击翻页
     * @param currentPage
     * @param request
     */
    @RequestMapping("getProjectByPage")
    public void getProjectByPage(int currentPage,HttpServletRequest request,HttpServletResponse response) throws IOException {
        Paging paging = (Paging) request.getSession().getAttribute("paging");
        paging.setFirstPage((currentPage-1)*paging.getLastPage());
        paging.setCountPage(currentPage);
        List<ProInfo> proInfoList = proInfoService.getProInfos(paging);
        response.getWriter().println(JSONArray.fromObject(paging).toString());
        response.getWriter().println(JSONArray.fromObject(proInfoList).toString());

    }

    /**
     * 通过项目名获取项目信息
     * @param pro_name
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("getProInfoByProName")
    public void getProInfoByProName(String pro_name,HttpServletRequest request, HttpServletResponse response)throws Exception{
        if(pro_name.equals("")) {
            response.getWriter().println("{\"message\":\"请输入项目名\"}");
            return;
        }
        ProInfo proinfo = proInfoService.getProInfoByProName(pro_name);
        System.out.println(proinfo);
        proInfoService.saveGetProInfoByProNameAction(pro_name);
        if(proinfo != null){
            response.getWriter().println(JSONArray.fromObject(proinfo).toString());
        }else{
            response.getWriter().println("{\"message\":\"没有项目信息(也许没有权限)\"}");
        }
    }

    /**
     * 获取状态下的所有项目信息
     * @param prostatus
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("getProInfoByProStatus")
    public void getProInfoByProStatus(int prostatus,HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<ProInfo> proInfoList = proInfoService.getProInfoByProStatus(prostatus);
        proInfoService.saveGetProInfoByProStatusAction(prostatus);
        if(proInfoList != null){
            request.getSession().setAttribute("proInfoList",proInfoList);
            response.getWriter().println(JSONArray.fromObject(proInfoList).toString());
        }else{
            response.getWriter().println("{\"message\":\"没有项目信息(也许没有权限)\"}");
        }
    }

    /**
     * 获取某一时间段的项目信息
     * @param prodate
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("getProInfoByProDate")
    public void getProInfoByProDate(Date prodate, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<ProInfo> proInfoList = proInfoService.getProInfoByProDate(prodate);
        if(proInfoList != null){
            response.getWriter().println(JSONArray.fromObject(proInfoList).toString());
        }else{
            response.getWriter().println("{\"message\":\"没有项目信息(也许没有权限)\"}");
        }
    }

    /**
     *  添加项目
     * @param response
     */
    @RequestMapping("saveProInfo")
    public void saveProInfo(ProInfo proinfo,HttpServletResponse response) throws Exception {

        if( proinfo.getPro_name() != null|| proinfo.getUser_id() != null) {
            proInfoService.saveProInfo(proinfo);
            proInfoService.saveSaveProInfoAction(proinfo);
            response.getWriter().println("{\"message\":\"ok\"}");
            return;
        }
        response.getWriter().println("{\"message\":\"创建项目失败!\"}");
    }

    /**
     * 更新项目名称
     * @param proinfo
     * @param response
     * @throws IOException
     */
    @RequestMapping("updateProInfoName")
    public void updateProInfoName(ProInfo proinfo,HttpServletResponse response) throws IOException {

        if(proinfo.getPro_name() != null){
            proInfoService.saveUpdateProInfoName(proinfo);
            proInfoService.updateProInfoName(proinfo);
            response.getWriter().println("{\"message\":\"ok\"}");
            return;
        }
        response.getWriter().println("{\"message\":\"更新项目名称失败!\"}");
    }

}
